//
// Table
//

package net.fauxjobuilder;

import java.util.*;

public class Table
{
    // ============================================================
    // Fields
    // ============================================================

    private String _schema;
    private String _rawName;
    private String _name;
    private Map<String,Column> _columns;
    private List<ForeignKey> _foreignKeys;

    // ============================================================
    // Constructors
    // ============================================================

    public Table( String schema, String rawName, String name )
    {
        _schema = schema;
        _rawName = rawName;
        _name = name;
        if ( _name == null )
        {
            _name = _rawName.toLowerCase();
        }

        _columns = new LinkedHashMap<String,Column>();
        _foreignKeys = new ArrayList<ForeignKey>();
    }

    // ============================================================
    // Methods
    // ============================================================

    public String getSchema()
    {
        return _schema;
    }

    public void setSchema( String schema )
    {
        _schema = schema;
    }

    public String getRawName()
    {
        return _rawName;
    }

    public void setRawName( String rawName )
    {
        _rawName = rawName;
    }

    public String getName()
    {
        return _name;
    }

    /**
     * Underscore plus first letter lowercase. e.g. _monkeyMan
     */
    public String getFieldName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toLowerCase( letter );

        return "_" + letter + _name.substring( 1 );
    }

    /**
     * First letter lowercase. e.g. monkeyMan
     */
    public String getArgName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toLowerCase( letter );

        return letter + _name.substring( 1 );
    }

    /**
     * First letter uppercase. e.g. MonkeyMan
     */
    public String getBeanName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return letter + _name.substring( 1 );
    }

    /**
     * getBeanName + "Home"
     */
    public String getHomeName()
    {
        return getBeanName() + "Home";
    }

    /**
     * "get" plus first letter uppercase. e.g. getMonkeyMan
     */
    public String getGetterName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return "get" + letter + _name.substring( 1 );
    }

    /**
     * getGetterName + "Home"
     */
    public String getHomeGetterName()
    {
        return getGetterName() + "Home";
    }

    /**
     * "set" plus first letter uppercase. e.g. getMonkeyMan
     */
    public String getSetterName()
    {
        char letter = _name.charAt( 0 );
        letter = Character.toUpperCase( letter );

        return "set" + letter + _name.substring( 1 );
    }

    public void setName( String prettyName )
    {
        _name = prettyName;
    }

    public void addColumn( Column column )
    {
        _columns.put( column.getRawName().toLowerCase(), column );
    }

    public Column getColumn( String columnName )
    {
        return _columns.get( columnName.toLowerCase() );
    }

    public Collection<Column> getColumns()
    {
        return _columns.values();
    }

    public void addForeignKey( ForeignKey fk )
    {
        _foreignKeys.add( fk );
    }

    public List<ForeignKey> getForeignKeys()
    {
        return _foreignKeys;
    }

    public Collection<ForeignKey> getForeignKeys( Column column )
    {
        HashSet<ForeignKey> fks = new HashSet<ForeignKey>();
        for ( ForeignKey fk : _foreignKeys )
        {
            if ( fk.getColumnName().equalsIgnoreCase( column.getRawName() ) )
            {
                fks.add( fk );
            }
        }

        return fks;
    }

    @Override
    public int hashCode()
    {
        return _rawName.hashCode();
    }

    @Override
    public boolean equals( Object other )
    {
        if ( other instanceof Table )
        {
            return _rawName.equals( ( (Table)other )._rawName );
        }
        return false;
    }
}

